package com.kbbywyclient.ui.fragment;

import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.LoansTableMenu;
import com.kbbywyclient.ui.adapter.MenuProductAdapter;
import com.kbbywyclient.ui.adapter.MenuProfessionAdapter;
import com.kbbywyclient.ui.adapter.MenuPurposeAdapter;
import com.kbbywyclient.ui.adapter.MenuScreenStatusAdapter;
import com.kbbywyclient.util.DateUtils;
import com.kbbywyclient.widget.MyGridView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Description:
 * Created by FHW
 * on 2017/4/21
 */

public class StatusDialogFragment extends DialogFragment {

    private String formatStr = "yyyy-MM-dd";

    private ListView lv_purpose;
    private ListView lv_product;
    private ListView lv_profession_info;
    private LinearLayout ll_screen;
    private EditText et_keyword;
    private MyGridView gv_status;
    private RadioGroup rg_apply_time;
    private RadioButton rb_one_month;
    private RadioButton rb_two_month;
    private RadioButton rb_three_month;
    private EditText et_start_time;
    private EditText et_end_time;

    private int mType = -1;

    private List<LoansTableMenu.LoansTypeBean> LoansTypeList = new ArrayList<>();//贷款用途
    private MenuPurposeAdapter menuPurposeAdapter = null;

    private List<LoansTableMenu.ProductListBean> productList = new ArrayList<>();//贷款产品
    private MenuProductAdapter menuProductAdapter = null;

    private List<LoansTableMenu.WorkTypeListBean> workTypeList = new ArrayList<>();
    private MenuProfessionAdapter menuProfessionAdapter = null;

    private List<LoansTableMenu.StatusBean> statusBeanList = new ArrayList<>();//处理状态
    private MenuScreenStatusAdapter menuScreenStatusAdapter = null;

    private String keyWord = "";//搜索关键字
    private String status = "";//订单状态Code集 ,多选用逗号隔开
    private long applyStarDateTime = 0;//时间段开始，int时间戳
    private long applyEndDateTime = 0;//时间段结束，int时间戳

    private int rbType = -1;

    public StatusDialogFragment(int mType, List<LoansTableMenu.LoansTypeBean> loansTypeList, List<LoansTableMenu.ProductListBean> productList, List<LoansTableMenu.WorkTypeListBean> workTypeList, List<LoansTableMenu.StatusBean> statusBeanList) {
        this.mType = mType;
        this.LoansTypeList.addAll(loansTypeList);
        this.productList.addAll(productList);
        this.workTypeList.addAll(workTypeList);
        this.statusBeanList.addAll(statusBeanList);
    }

    public StatusDialogFragment(int mType, List<LoansTableMenu.LoansTypeBean> loansTypeList, List<LoansTableMenu.ProductListBean> productList, List<LoansTableMenu.WorkTypeListBean> workTypeList, List<LoansTableMenu.StatusBean> statusBeanList
            , int rbType, long applyStarDateTime, long applyEndDateTime) {
        this.mType = mType;
        this.LoansTypeList.addAll(loansTypeList);
        this.productList.addAll(productList);
        this.workTypeList.addAll(workTypeList);
        this.statusBeanList.addAll(statusBeanList);
        this.rbType = rbType;
        this.applyStarDateTime = applyStarDateTime;
        this.applyEndDateTime = applyEndDateTime;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这句代码的意思是让dialogFragment弹出时沾满全屏
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_DialogWhenLarge_NoActionBar);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        View view = inflater.inflate(R.layout.pop_status_modify_dialog, container);
        View view = inflater.inflate(R.layout.pop_bought_table, container);
        //让DialogFragment的背景为透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initView(view);

        showView(mType);

        return view;
    }

    private void initView(View view) {
        lv_purpose = (ListView) view.findViewById(R.id.lv_purpose);
        lv_product = (ListView) view.findViewById(R.id.lv_product);
        lv_profession_info = (ListView) view.findViewById(R.id.lv_profession_info);
        ll_screen = (LinearLayout) view.findViewById(R.id.ll_screen);

        et_keyword = (EditText) view.findViewById(R.id.et_keyword);
        gv_status = (MyGridView) view.findViewById(R.id.gv_status);
        rg_apply_time = (RadioGroup) view.findViewById(R.id.rg_apply_time);
        rb_one_month = (RadioButton) view.findViewById(R.id.rb_one_month);
        rb_two_month = (RadioButton) view.findViewById(R.id.rb_two_month);
        rb_three_month = (RadioButton) view.findViewById(R.id.rb_three_month);
        et_start_time = (EditText) view.findViewById(R.id.et_start_time);
        et_end_time = (EditText) view.findViewById(R.id.et_end_time);
        TextView tv_reset = (TextView) view.findViewById(R.id.tv_reset);
        TextView tv_determine = (TextView) view.findViewById(R.id.tv_determine);

        View v_dimess = view.findViewById(R.id.v_dimess);

        LinearLayout ll_purpose = (LinearLayout) view.findViewById(R.id.ll_purpose);
        LinearLayout ll_product = (LinearLayout) view.findViewById(R.id.ll_product);
        LinearLayout ll_profession_info = (LinearLayout) view.findViewById(R.id.ll_profession_info);
        LinearLayout ll_screen_bt = (LinearLayout) view.findViewById(R.id.ll_screen_bt);

        ll_purpose.setOnClickListener(onClickListener);
        ll_product.setOnClickListener(onClickListener);
        ll_profession_info.setOnClickListener(onClickListener);
        ll_screen_bt.setOnClickListener(onClickListener);

        v_dimess.setOnClickListener(v -> {
                    dismiss();
                    EventBus.getDefault().post(new Event.DialogDismess());
                }
        );

        lv_purpose.setOnItemClickListener((parent, view1, position, id) -> {
            dismiss();
            for (int i = 0; i < LoansTypeList.size(); i++) {
                LoansTypeList.get(i).setCheck(false);
                if (position == i) {
                    LoansTypeList.get(i).setCheck(true);
//                    loansType = LoansTypeList.get(i).getID() + "";
//                    ViewUtils.setText(tv_purpose, LoansTypeList.get(i).getName());
                }
            }
//            ptr_view.autoRefresh();
            EventBus.getDefault().post(new Event.RefreshList(mType, position));
        });
        lv_product.setOnItemClickListener((parent, view1, position, id) -> {
            dismiss();
            for (int i = 0; i < productList.size(); i++) {
                productList.get(i).setCheck(false);
                if (position == i) {
                    productList.get(i).setCheck(true);
//                    productLoansID = productList.get(i).getID() + "";
//                    ViewUtils.setText(tv_product, productList.get(i).getName());
                }
            }
//            ptr_view.autoRefresh();
            EventBus.getDefault().post(new Event.RefreshList(mType, position));
        });
        lv_profession_info.setOnItemClickListener((parent, view12, position, id) -> {
            dismiss();
            for (int i = 0; i < workTypeList.size(); i++) {
                workTypeList.get(i).setCheck(false);
                if (position == i) {
                    workTypeList.get(i).setCheck(true);
//                    workTypeCode = workTypeList.get(i).getCode();
//                    ViewUtils.setText(tv_profession_info, workTypeList.get(i).getName());
                }
            }
//            ptr_view.autoRefresh();
            EventBus.getDefault().post(new Event.RefreshList(mType, position));
        });

        //记录上传选择的时间
        if (rbType == -1) {
            if (applyStarDateTime != 0) {
                et_start_time.setText(DateUtils.getDateToString(applyStarDateTime));
            }
            if (applyEndDateTime != 0) {
                et_end_time.setText(DateUtils.getDateToString(applyEndDateTime));
            }
        }
        switch (rbType) {
            case 0:
                chooseTimeView(R.id.rb_one_month);
                break;
            case 1:
                chooseTimeView(R.id.rb_two_month);
                break;
            case 2:
                chooseTimeView(R.id.rb_three_month);
                break;
        }
        rg_apply_time.setOnCheckedChangeListener((group, checkedId) -> {
            chooseTimeView(checkedId);
        });
        et_start_time.setOnClickListener(onClickListener);
        et_end_time.setOnClickListener(onClickListener);
        tv_reset.setOnClickListener(onClickListener);
        tv_determine.setOnClickListener(onClickListener);
        //        //筛选
        et_keyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                keyWord = s.toString();
            }
        });


    }

    private void chooseTimeView(int checkedId) {
        rb_one_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
        rb_two_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
        rb_three_month.setBackgroundResource(R.drawable.shape_frame_line_dddddd);
        rb_one_month.setTextColor(getResources().getColor(R.color.color_212121));
        rb_two_month.setTextColor(getResources().getColor(R.color.color_212121));
        rb_three_month.setTextColor(getResources().getColor(R.color.color_212121));
        if (rbType != -1) {
            applyEndDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(0, formatStr));
        }
        switch (checkedId) {
            case R.id.rb_one_month:
                rbType = 0;
                rb_one_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
                rb_one_month.setTextColor(getResources().getColor(R.color.color_ffffff));
                setClearCustomDate();
                applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(30, formatStr));
                break;
            case R.id.rb_two_month:
                rbType = 1;
                rb_two_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
                rb_two_month.setTextColor(getResources().getColor(R.color.color_ffffff));
                setClearCustomDate();
                applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(60, formatStr));
                break;
            case R.id.rb_three_month:
                rbType = 2;
                rb_three_month.setBackgroundResource(R.drawable.textview_bg_14c095_6px);
                rb_three_month.setTextColor(getResources().getColor(R.color.color_ffffff));
                setClearCustomDate();
                applyStarDateTime = DateUtils.getStringToDate(DateUtils.getAnyData(90, formatStr));
                break;
        }
    }

    private void setPopLvVisbilety() {
        lv_purpose.setVisibility(View.GONE);
        lv_product.setVisibility(View.GONE);
        lv_profession_info.setVisibility(View.GONE);
        ll_screen.setVisibility(View.GONE);
    }

    private View.OnClickListener onClickListener = v -> {
        switch (v.getId()) {
            case R.id.et_start_time:
                Calendar startC = Calendar.getInstance();
                new DatePickerDialog(getActivity(), (view, year, month, dayOfMonth) -> {
                    rg_apply_time.clearCheck();
                    applyStarDateTime = DateUtils.getStringToDate(year + "-" + (++month) + "-" + dayOfMonth);
                    et_start_time.setText(year + "-" + month + "-" + dayOfMonth);
                    rbType = -1;
                }, startC.get(Calendar.YEAR), startC.get(Calendar.MONTH), startC
                        .get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.et_end_time:
                Calendar endC = Calendar.getInstance();
                new DatePickerDialog(getActivity(), (view, year, month, dayOfMonth) -> {
                    rg_apply_time.clearCheck();
                    applyEndDateTime = DateUtils.getStringToDate(year + "-" + (++month) + "-" + dayOfMonth);
                    et_end_time.setText(year + "-" + month + "-" + dayOfMonth);
                    rbType = -1;
                }, endC.get(Calendar.YEAR), endC.get(Calendar.MONTH), endC
                        .get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.tv_reset:
                keyWord = "";
                et_keyword.setText(keyWord);

                for (LoansTableMenu.StatusBean statusBean : statusBeanList) {
                    statusBean.setCheck(false);
                }
                menuScreenStatusAdapter.notifyDataSetChanged();

                rbType = -1;
                rg_apply_time.clearCheck();

                applyStarDateTime = 0;
                applyEndDateTime = 0;
                setClearCustomDate();

                break;
            case R.id.tv_determine://确定
                dismiss();
                status = "";
                for (LoansTableMenu.StatusBean statusBean : statusBeanList) {
                    if (statusBean.isCheck()) {
                        status += statusBean.getCode() + ",";
                    }
                }
                EventBus.getDefault().post(new Event.RefreshList(mType, keyWord, status, applyStarDateTime, applyEndDateTime, rbType));
                break;
            case R.id.ll_purpose:
                mType = 0;
                showView(mType);
                break;
            case R.id.ll_product:
                mType = 1;
                showView(mType);
                break;
            case R.id.ll_profession_info:
                mType = 2;
                showView(mType);
                break;
            case R.id.ll_screen_bt:
                mType = 3;
                showView(mType);
                break;
        }
    };

    private void setClearCustomDate() {
        et_start_time.setText("");
        et_end_time.setText("");
    }

    private void showView(int mType) {
        EventBus.getDefault().post(new Event.ShowTopView(mType));
        setPopLvVisbilety();
        switch (mType) {
            case 0://用途
                lv_purpose.setVisibility(View.VISIBLE);
                if (LoansTypeList != null) {
                    setPurposeAdapter(lv_purpose);
                }
                break;
            case 1://产品
                lv_product.setVisibility(View.VISIBLE);
                if (productList != null) {
                    setProductAdapter(lv_product);
                }
                break;
            case 2://职位
                lv_profession_info.setVisibility(View.VISIBLE);
                if (workTypeList != null) {
                    setProfessionAdapter(lv_profession_info);
                }
                break;
            case 3://筛选
//                keyWord = "";
//                rbType = -1;
//                applyStarDateTime = 0;
//                applyEndDateTime = 0;
                ll_screen.setVisibility(View.VISIBLE);
                if (statusBeanList != null) {
                    setStatusAdapter(gv_status);
                }
                break;
        }
    }

    /**
     * 设置贷款用途 菜单数据
     *
     * @param lv
     */
    private void setPurposeAdapter(ListView lv) {
        menuPurposeAdapter = new MenuPurposeAdapter(getActivity(), R.layout.item_menu_purpose, LoansTypeList);
        lv.setAdapter(menuPurposeAdapter);
    }

    /**
     * 设置贷款产品 菜单数据
     *
     * @param lv
     */
    private void setProductAdapter(ListView lv) {
        menuProductAdapter = new MenuProductAdapter(getActivity(), R.layout.item_menu_purpose, productList);
        lv.setAdapter(menuProductAdapter);
    }

    private void setProfessionAdapter(ListView lv) {
        menuProfessionAdapter = new MenuProfessionAdapter(getActivity(), R.layout.item_menu_purpose, workTypeList);
        lv.setAdapter(menuProfessionAdapter);
    }

    private void setStatusAdapter(MyGridView gv) {
        menuScreenStatusAdapter = new MenuScreenStatusAdapter(getActivity(), R.layout.item_menu_status, statusBeanList);
        gv.setAdapter(menuScreenStatusAdapter);
    }

}
